Skip to content

SliceFilterProxyModel

Base classes

Name Children Inherits
FlatProxyMixin
prettyqt.itemmodels.proxies.flatproxymodel
Mixin for proxies which reshape a source table into another flat table.
SliceIdentityProxyModel
prettyqt.itemmodels.proxies.sliceidentityproxymodel
Base model for slice proxy models.

â‹” Inheritance diagram

graph TD
  941984971["itemmodels.SliceFilterProxyModel"]
  445963150["itemmodels.FlatProxyMixin"]
  2751680898["builtins.object"]
  4214724289["itemmodels.SliceIdentityProxyModel"]
  527136029["core.IdentityProxyModel"]
  2431222121["core.AbstractProxyModelMixin"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  3668892142["QtCore.QIdentityProxyModel"]
  3989152870["QtCore.QAbstractProxyModel"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  445963150 --> 941984971
  2751680898 --> 445963150
  4214724289 --> 941984971
  527136029 --> 4214724289
  2431222121 --> 527136029
  3882849576 --> 2431222121
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  3668892142 --> 527136029
  3989152870 --> 3668892142
  1898060022 --> 3989152870
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: FlatProxyMixin, SliceIdentityProxyModel

Proxy model to filter an item view based on python slicing syntax.

Since slicing operations are bijective, this model can filter without looping through rows or columns. Thus, this should perform much better than a SortFilterProxyModel with a column filter. (O(1) instead of O(n))

Example

To filter out every second row, and cut off the the first two columns:

model = MyModel()
table = widgets.TableView()
table.set_model(model)
table.proxifier[::2, 2:].filter()
table.show()
# or
indexer = (slice(None, None, 2), slice(2, None))
proxy = itemmodels.SliceFilterProxyModel(indexer=indexer)
proxy.set_source_model(model)
table.set_model(proxy)
table.show()
Source code in prettyqt/itemmodels/proxies/slicefilterproxymodel.py
class SliceFilterProxyModel(FlatProxyMixin, itemmodels.SliceIdentityProxyModel):
    """Proxy model to filter an item view based on python slicing syntax.

    Since slicing operations are bijective, this model can filter without
    looping through rows or columns. Thus, this should perform much better than a
    SortFilterProxyModel with a column filter. (O(1) instead of O(n))

    ### Example

    To filter out every second row, and cut off the the first two columns:

    ```py
    model = MyModel()
    table = widgets.TableView()
    table.set_model(model)
    table.proxifier[::2, 2:].filter()
    table.show()
    # or
    indexer = (slice(None, None, 2), slice(2, None))
    proxy = itemmodels.SliceFilterProxyModel(indexer=indexer)
    proxy.set_source_model(model)
    table.set_model(proxy)
    table.show()
    ```
    """

    ID = "slice_filter"
    ICON = "mdi.table-filter"

    def rowCount(self, parent: core.ModelIndex | None = None) -> int:
        parent = parent or core.ModelIndex()
        if parent.isValid() or self.sourceModel() is None:
            return 0
        return len(self.get_row_range())

    def columnCount(self, parent: core.ModelIndex | None = None) -> int:
        parent = parent or core.ModelIndex()
        if parent.isValid() or self.sourceModel() is None:
            return 0
        return len(self.get_column_range())

    def headerData(
        self,
        section: int,
        orientation: constants.Orientation,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        """Map header data to proxy by calculating position from slice values.

        source pos = slice start + proxy pos * slice step)
        """
        is_horizontal = orientation == constants.HORIZONTAL
        rng = self.get_column_range() if is_horizontal else self.get_row_range()
        if not 0 <= section < len(rng) or self.sourceModel() is None:
            return None
        return self.sourceModel().headerData(rng[section], orientation, role)

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        return self.mapToSource(index).data(role) if index.isValid() else None

    def setData(
        self,
        index: core.ModelIndex,
        value,
        role: constants.ItemDataRole = constants.EDIT_ROLE,
    ) -> bool:
        source_index = self.mapToSource(index)
        if not source_index.isValid():
            return False
        return self.sourceModel().setData(source_index, value, role)

    def flags(self, index: core.ModelIndex) -> constants.ItemFlag:
        source_index = self.mapToSource(index)
        if not source_index.isValid():
            return constants.NO_FLAGS
        return self.sourceModel().flags(source_index)

    def mapToSource(self, proxy_idx: core.ModelIndex) -> core.ModelIndex:
        """Map index to source by calculating position from slice values.

        source pos = slice start + proxy pos * slice step)
        """
        source = self.sourceModel()
        if source is None or not proxy_idx.isValid():
            return core.ModelIndex()
        col_range = self.get_column_range()
        row_range = self.get_row_range()
        if proxy_idx.row() >= len(row_range) or proxy_idx.column() >= len(col_range):
            return core.ModelIndex()
        return source.index(row_range[proxy_idx.row()], col_range[proxy_idx.column()])

    def mapFromSource(self, source_index: core.ModelIndex) -> core.ModelIndex:
        """Map index from source by calculating position based on slice values.

        proxy pos = source pos - slice start / slice step
        """
        if self.sourceModel() is None or not source_index.isValid():
            return core.ModelIndex()
        if source_index.parent().isValid():
            return core.ModelIndex()
        row_range = self.get_row_range()
        col_range = self.get_column_range()
        if source_index.row() not in row_range or source_index.column() not in col_range:
            return core.ModelIndex()
        return self.index(
            row_range.index(source_index.row()), col_range.index(source_index.column())
        )

    def set_indexer(self, indexer):
        super().set_indexer(indexer)
        # model might not be initialized yet when called from __init__
        if self.sourceModel() is not None:
            self.force_reset()

headerData(section: int, orientation: constants.Orientation, role: constants.ItemDataRole = constants.DISPLAY_ROLE)

Map header data to proxy by calculating position from slice values.

source pos = slice start + proxy pos * slice step)

Source code in prettyqt/itemmodels/proxies/slicefilterproxymodel.py
def headerData(
    self,
    section: int,
    orientation: constants.Orientation,
    role: constants.ItemDataRole = constants.DISPLAY_ROLE,
):
    """Map header data to proxy by calculating position from slice values.

    source pos = slice start + proxy pos * slice step)
    """
    is_horizontal = orientation == constants.HORIZONTAL
    rng = self.get_column_range() if is_horizontal else self.get_row_range()
    if not 0 <= section < len(rng) or self.sourceModel() is None:
        return None
    return self.sourceModel().headerData(rng[section], orientation, role)

mapFromSource(source_index: core.ModelIndex) -> core.ModelIndex

Map index from source by calculating position based on slice values.

proxy pos = source pos - slice start / slice step

Source code in prettyqt/itemmodels/proxies/slicefilterproxymodel.py
def mapFromSource(self, source_index: core.ModelIndex) -> core.ModelIndex:
    """Map index from source by calculating position based on slice values.

    proxy pos = source pos - slice start / slice step
    """
    if self.sourceModel() is None or not source_index.isValid():
        return core.ModelIndex()
    if source_index.parent().isValid():
        return core.ModelIndex()
    row_range = self.get_row_range()
    col_range = self.get_column_range()
    if source_index.row() not in row_range or source_index.column() not in col_range:
        return core.ModelIndex()
    return self.index(
        row_range.index(source_index.row()), col_range.index(source_index.column())
    )

mapToSource(proxy_idx: core.ModelIndex) -> core.ModelIndex

Map index to source by calculating position from slice values.

source pos = slice start + proxy pos * slice step)

Source code in prettyqt/itemmodels/proxies/slicefilterproxymodel.py
def mapToSource(self, proxy_idx: core.ModelIndex) -> core.ModelIndex:
    """Map index to source by calculating position from slice values.

    source pos = slice start + proxy pos * slice step)
    """
    source = self.sourceModel()
    if source is None or not proxy_idx.isValid():
        return core.ModelIndex()
    col_range = self.get_column_range()
    row_range = self.get_row_range()
    if proxy_idx.row() >= len(row_range) or proxy_idx.column() >= len(col_range):
        return core.ModelIndex()
    return source.index(row_range[proxy_idx.row()], col_range[proxy_idx.column()])

:fa-brands-github: Show source on GitHub